/*******************************************************************************
 * Level Master 2000
 * Copyright (C) 2010 Christian Herzog
 * For more information see http://code.google.com/p/levelmaster2000/
 *
 * This file is licensed under the terms of the MIT license, see
 * http://www.opensource.org/licenses/mit-license.php for further information.
 ******************************************************************************/

#include "ComponentBrowser.h"

#include "ui_ComponentBrowser.h"

#include <QPushButton>
#include <QLineEdit>
#include <QMessageBox>
#include <QInputDialog>
#include <QDialog>
#include <QCompleter>
#include <QStandardItemModel>
#include <QModelIndex>


ComponentBrowser::ComponentBrowser( QWidget *parent, QList<ClassInfo> &componentInfo )
    : QDockWidget( parent )
{
    init( componentInfo );
}

void ComponentBrowser::init( QList<ClassInfo> &componentInfo )
{
    //componentBrowserWidget = new QDockWidget(this);
    Ui::ComponentBrowserWidget UiComponentBrowserWidget;
    UiComponentBrowserWidget.setupUi( this );
    this->hide();

    tree = this->findChild<QTreeWidget*>( "componentBrowser" );

    //Connect to clicked event. This will take care of en/disabling the add button.
    connect( tree,
            SIGNAL( itemClicked( QTreeWidgetItem*, int ) ),
            this,
            SLOT( itemClicked( QTreeWidgetItem*, int ) ) );


    //Fill the component browser with data:
    QTreeWidgetItem *item = 0;

    foreach( ClassInfo info, componentInfo )
    {
        QString fullClassName;
        //foreach( QString package, info.package )
        for( int pIdx = 1; pIdx < info.package.size(); pIdx++ )
        {
            fullClassName += info.package[pIdx] + ".";
        }
        fullClassName += info.name;

        //Check if class is in a package:
        if( info.package.size() > 0 )
        {
            item = 0;

            bool foundTopLevelItem = false;
            for( int i = 0; i < tree->topLevelItemCount(); i++ )
            {
                item = tree->topLevelItem( i );
                if( item->text( 0 ) == info.package[0] )
                {
                    foundTopLevelItem = true;
                    break;
                }
            }

            if( !foundTopLevelItem )
            {
                item = new QTreeWidgetItem();
                item->setText( 0, info.package[0] );
                tree->addTopLevelItem( item );
            }

            buildComponentBrowser( item, fullClassName );
        }
        //Class isn't in a package:
        else
        {
            item = new QTreeWidgetItem();
            item->setText( 0, info.name );
            tree->addTopLevelItem( item );
       }
    }

    tree->sortByColumn(0, Qt::AscendingOrder);

    //Connect the add component button:
    QPushButton *addButton = this->findChild<QPushButton*>( "componentBrowserAddButton" );
    connect( addButton,
            SIGNAL( clicked() ),
            this,
            SLOT( addClicked() ) );

    //At starting up, we disable the button, as nothing is selected.
    addButton->setEnabled( false );
}

void ComponentBrowser::itemClicked( QTreeWidgetItem *item, int column )
{
    QPushButton *addButton = this->findChild<QPushButton*>( "componentBrowserAddButton" );
    if( 0 == addButton  )
    {
        QMessageBox::critical( this,
                "LevelMaster2000::handleClickedComponentBrowser",
                "Widget \"componentBrowserAddButton\" not found." );
        return;
    }

    if( item->childCount() > 0 )
    {
        addButton->setEnabled( false );
    }
    else
    {
        addButton->setEnabled( true );
    }
}

QString ComponentBrowser::buildFullClassName( QTreeWidgetItem *item, bool isClass )
{
    QString className;

    if( 0 != item->parent() )
    {
        className += buildFullClassName( item->parent(), false );
        if( isClass )
        {
            className += "::";
        }
        else
        {
            className += ".";
        }
    }

    className += item->text( 0 );

    return className;
}


void ComponentBrowser::addClicked()
{
    QString componentName = this->findChild<QLineEdit*>( "componentNameLineEdit" )->text();

    foreach( QTreeWidgetItem *item, tree->selectedItems() )
    {
        //Get the full class name:
        QString className = buildFullClassName( item, true );

        if( componentName == "" )
        {
            componentName = className;
        }

        emit createComponent( componentName, className );
    }
}


void ComponentBrowser::buildComponentBrowser( QTreeWidgetItem *parent, QString package )
{
    //string of form "com.pblabs.rendering.xxx.yyy.CLASS"
    if( !package.isEmpty() )
    {
        QStringList splitPackage = package.split(".");

        if( splitPackage.size() > 0 )
        {
            QTreeWidgetItem *item = 0;

            bool foundTopLevelItem = false;
            for( int i = 0; i < parent->childCount(); i++ )
            {
                item = parent->child( i );
                if( item->text( 0 ) == splitPackage[0] )
                {
                    foundTopLevelItem = true;
                    break;
                }
            }

            if( !foundTopLevelItem )
            {
                item = new QTreeWidgetItem( parent );
            }

            item->setText( 0, splitPackage[0] );

            package.remove( 0, splitPackage[0].size() + 1 ); //+1 is needed for "."

            //Recursive call to build up whole tree:
            buildComponentBrowser( item, package );
        }
    }
}
